<?php
// 本类由系统自动生成，仅供测试用途
class PlusAction extends CommonAction {

    /**
     * 首页
     */
     public function Index(){
          $plus = D('Plus')->getPlusListByPage($_REQUEST);
          $this->plus = $plus;
          $this->display();
     }

     /**
      * 添加插件
      */
     public function plus_add(){
          if(IS_POST){
               $res = D('Plus')->addPlus($_POST);
               $op = $_POST['id']?'修改':'添加';
               if($res['status']==200){
                   $this->show("success",U('admin/Plus/Index'),$op.'成功');
               }else{
                   $this->show("error",U('admin/Plus/plus_add'),$op.'成功');
               }
          }
          $type = M('plus_type')->select();
          if($_GET['id']){
              $plus = D("Plus")->getPlusByOne(array('id'=>$_GET['id']));
              $plus['typename'] = M('plus_type')->where(array('id'=>$plus['type']))->getField('name');
              $this->plus = $plus;
          }
          $this->type = $type;
          $this->display();
     }

    /**
     * 插件类别列表
     */
    public function plus_type(){
        $Page = $_GET['page']?$_GET['page']:1;
        $pageSize = 10;
        $startLimit = ($Page-1)*$pageSize;
        $where['uid'] = cookie('loginId');
        $typeCount = M('plus_type')->where($where)->count();
        $typeList = M('plus_type')->where($where)->limit($startLimit,$pageSize)->select();
        if($typeCount>$pageSize){
            $url = "__URL__/media_type?";
            $pagestr = $this->page($typeCount,$Page,$pageSize,$url);
            $this->pagestr = $pagestr;
        }
        $this->typecount = $typeCount;
        $this->typelist = $typeList;
        $this->display();
    }

    /**
     * 添加类别列表
     */
    public function plus_type_add(){
        if(IS_POST){
            $type = I('post.type_name');
            // 查询分类是否存在
            $where['name'] = $type;
            $res = M('plus_type')->where($where)->find();
            if($res){
                $this->show("error",U('admin/Media/media_type_add'),'该分类已存在');
                die;
            }
            $inserData['name'] = $type;
            $inserData['uid']  = cookie('loginId');
            $inserData['created_at'] = time();
            if($_POST['id']){
                $res = M('plus_type')->where(array('id'=>$_POST['id']))->save($inserData);
                $tips = "修改";
            } else {
                $res = M('plus_type')->add($inserData);
                $tips = '添加';
            }
            if($res){
                $this->show("success",U('admin/Plus/plus_type'),'分类'.$tips.'成功');
            }else{
                $this->show("error",U('admin/Plus/plus_type'),'分类'.$tips.'失败');
            }
        }
        if($_GET['id']){
            $typeInfo = M('plus_type')->find($_GET['id']);
            if(!$typeInfo){
                $this->show("error",U('admin/Plus/plus_type'),'分类信息错误');
            }
            $this->typeinfo = $typeInfo;
        }
        $this->display();
    }

     /**
      *  删除插件
      */
     public function plus_del(){
          $res = D('Plus')->delPlus($_GET['id']);
          echo json_encode($res);
     }
}